package com.zhentao.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhentao.dto.TaskApprovalDTO;
import com.zhentao.dto.WorkflowStartDTO;
import com.zhentao.pojo.WorkflowHistory;
import com.zhentao.pojo.WorkflowTask;
import com.zhentao.service.WorkflowHistoryService;
import com.zhentao.service.WorkflowTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 流程任务Controller
 */
@RestController
@RequestMapping("/produce/workflow/task")
public class WorkflowTaskController {
    
    @Autowired
    private WorkflowTaskService workflowTaskService;
    
    @Autowired
    private WorkflowHistoryService workflowHistoryService;
    
    /**
     * 分页查询任务列表
     */
    @GetMapping("/list")
    public Map<String, Object> list(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String billType,
            @RequestParam(required = false) Integer status) {
        
        try {
            Page<WorkflowTask> page = new Page<>(current, size);
            QueryWrapper<WorkflowTask> wrapper = new QueryWrapper<>();
            
            if (billType != null && !billType.isEmpty()) {
                wrapper.eq("bill_type", billType);
            }
            if (status != null) {
                wrapper.eq("status", status);
            }
            
            wrapper.orderByDesc("start_time");
            Page<WorkflowTask> result = workflowTaskService.page(page, wrapper);
            
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("message", "查询成功");
            response.put("data", result);
            
            return response;
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("code", 500);
            response.put("message", "查询失败: " + e.getMessage());
            response.put("error", e.toString());
            return response;
        }
    }
    
    /**
     * 根据ID查询任务
     */
    @GetMapping("/getById/{id}")
    public Map<String, Object> getById(@PathVariable Long id) {
        WorkflowTask task = workflowTaskService.getById(id);
        List<WorkflowHistory> history = workflowHistoryService.getHistoryByTaskId(id);
        
        Map<String, Object> data = new HashMap<>();
        data.put("task", task);
        data.put("history", history);
        
        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("message", "查询成功");
        response.put("data", data);
        
        return response;
    }
    
    /**
     * 启动工作流
     */
    @PostMapping("/start")
    public Map<String, Object> startWorkflow(@RequestBody WorkflowStartDTO dto) {
        boolean result = workflowTaskService.startWorkflow(
            dto.getProcessId(), 
            dto.getBillType(), 
            dto.getBillId(), 
            dto.getUserId()
        );
        
        Map<String, Object> response = new HashMap<>();
        if (result) {
            response.put("code", 200);
            response.put("message", "工作流启动成功");
        } else {
            response.put("code", 500);
            response.put("message", "工作流启动失败");
        }
        
        return response;
    }
    
    /**
     * 审批任务
     */
    @PostMapping("/approve")
    public Map<String, Object> approveTask(@RequestBody TaskApprovalDTO dto) {
        boolean result = workflowTaskService.approveTask(
            dto.getTaskId(), 
            dto.getUserId(), 
            dto.getAction(), 
            dto.getRemark()
        );
        
        Map<String, Object> response = new HashMap<>();
        if (result) {
            response.put("code", 200);
            response.put("message", "审批成功");
        } else {
            response.put("code", 500);
            response.put("message", "审批失败");
        }
        
        return response;
    }
    
    /**
     * 撤回任务
     */
    @PostMapping("/revoke")
    public Map<String, Object> revokeTask(@RequestBody TaskApprovalDTO dto) {
        boolean result = workflowTaskService.revokeTask(
            dto.getTaskId(), 
            dto.getUserId(), 
            dto.getRemark()
        );
        
        Map<String, Object> response = new HashMap<>();
        if (result) {
            response.put("code", 200);
            response.put("message", "撤回成功");
        } else {
            response.put("code", 500);
            response.put("message", "撤回失败");
        }
        
        return response;
    }
    
    /**
     * 查询待办任务
     */
    @GetMapping("/todo/{assigneeId}")
    public Map<String, Object> getTodoTasks(@PathVariable Long assigneeId) {
        List<WorkflowTask> tasks = workflowTaskService.getTodoTasksByAssignee(assigneeId);
        
        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("message", "查询成功");
        response.put("data", tasks);
        
        return response;
    }
    
    /**
     * 查询已办任务
     */
    @GetMapping("/done/{assigneeId}")
    public Map<String, Object> getDoneTasks(@PathVariable Long assigneeId) {
        List<WorkflowTask> tasks = workflowTaskService.getDoneTasksByAssignee(assigneeId);
        
        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("message", "查询成功");
        response.put("data", tasks);
        
        return response;
    }
    
    /**
     * 根据单据查询任务
     */
    @GetMapping("/byBill")
    public Map<String, Object> getTasksByBill(
            @RequestParam String billType,
            @RequestParam Long billId) {
        List<WorkflowTask> tasks = workflowTaskService.getTasksByBill(billType, billId);
        
        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("message", "查询成功");
        response.put("data", tasks);
        
        return response;
    }
} 